package com.sali.service.data_file_uploader.refcount;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 封装源文件引用计数信息
 * @author sali
 * @date 2025/3/16 22:00
 */
public class ReferenceCounter {

    // 源文件路径，用于找到源文件以执行删除
    private String sourceFilePath;

    // 源文件唯一标识
    private String sourceFileId;

    // 源文件中的总记录数
    private long total = 0;

    // 被过滤的记录数，存在多线程修改
    private AtomicLong discardRecordNum;

    // 输出记录数，存在多线程的修改
    private AtomicLong outputRecordNum;

    // 标记源文件中记录数是否已被读完
    private volatile boolean readCompleted = false;

    // true: 标志该源文件处理出现异常，不再进行后续处理
    private volatile boolean occurError = false;

    public ReferenceCounter(String sourceFilePath, String sourceFileId) {
        this.sourceFilePath = sourceFilePath;
        this.sourceFileId = sourceFileId;
        this.discardRecordNum = new AtomicLong(0);
        this.outputRecordNum = new AtomicLong(0);
    }

    // 过滤记录条数增加计数
    public void discardRecordInc() {
        this.discardRecordNum.incrementAndGet();
    }

    // 输出记录条数增加计数
    public void outputRecordInc() {
        this.outputRecordNum.incrementAndGet();
    }

    // 源文件中的数据是否已经读完
    public boolean hasReadCompleted() {
        return this.readCompleted;
    }

    // 判断源文件中的记录是否已经处理完毕： total = discardRecordNum + outputRecordNum
    public boolean hasProcessed() {
        return this.readCompleted && total == (discardRecordNum.intValue() + outputRecordNum.intValue());
    }

    public String getSourceFilePath() {
        return sourceFilePath;
    }

    public void setSourceFilePath(String sourceFilePath) {
        this.sourceFilePath = sourceFilePath;
    }

    public String getSourceFileId() {
        return sourceFileId;
    }

    public void setSourceFileId(String sourceFileId) {
        this.sourceFileId = sourceFileId;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public AtomicLong getDiscardRecordNum() {
        return discardRecordNum;
    }

    public void setDiscardRecordNum(AtomicLong discardRecordNum) {
        this.discardRecordNum = discardRecordNum;
    }

    public AtomicLong getOutputRecordNum() {
        return outputRecordNum;
    }

    public void setOutputRecordNum(AtomicLong outputRecordNum) {
        this.outputRecordNum = outputRecordNum;
    }

    public boolean isReadCompleted() {
        return readCompleted;
    }

    public void setReadCompleted(boolean readCompleted) {
        this.readCompleted = readCompleted;
    }

    public boolean isOccurError() {
        return occurError;
    }

    public void setOccurError(boolean occurError) {
        this.occurError = occurError;
    }
}
